package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.dao.ConventionDao;
import it.asvp.asvp_portal.data.dao.MemberAccountDao;
import it.asvp.asvp_portal.data.dao.MemberDao;
import it.asvp.asvp_portal.data.entity.domain.Address;
import it.asvp.asvp_portal.data.entity.domain.Convention;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.data.entity.member.MemberAccount;
import it.asvp.asvp_portal.service.services.ConventionService;
import it.asvp.asvp_portal.service.services.MemberAccountService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ConventionServiceImpl implements ConventionService {

	@Autowired
	ConventionDao conventionDao;
	@Autowired
	MemberDao memberDao;
	@Autowired
	MemberAccountDao memberAccountDao;
	@Autowired
	MemberAccountService memberAccountService;
	
	@Transactional
	@Rollback
	public Long createConvention(UserDetails userDetails,Convention transientConvention) throws Exception{
		
		MemberAccount persistentCurrentAccount=memberAccountDao.findByLogin(userDetails.getUsername());
		  
		if(persistentCurrentAccount==null){
				throw new Exception("Account errato");
		}
		
		Member persistentMember=persistentCurrentAccount.getMember();
		transientConvention.setMember(persistentMember);
		return conventionDao.persist(transientConvention);
	}
	
	@Transactional
	@Rollback
	public void updateConvention(UserDetails userDetails, Long idConventionToUpdate, Convention transientConvention) throws Exception{
		
		MemberAccount persistentCurrentAccount=memberAccountDao.findByLogin(userDetails.getUsername());
		  
		if(persistentCurrentAccount==null){
				throw new Exception("Account errato");
		}
		
		Convention persistentConvention=conventionDao.findById(idConventionToUpdate);
		
		if(persistentConvention==null){
			throw new Exception("Convenzione non presente");
		}
		
		Address persistentAddress=persistentConvention.getAddress();
        persistentAddress.setCity(transientConvention.getAddress().getCity());
        persistentAddress.setCivicNumber(transientConvention.getAddress().getCivicNumber());
        persistentAddress.setLastUpdate(DateUtils.getCurrentDate());
        persistentAddress.setPostalCode(transientConvention.getAddress().getPostalCode());
        persistentAddress.setProvince(transientConvention.getAddress().getProvince());
        persistentAddress.setStreet(transientConvention.getAddress().getStreet());
        
        persistentConvention.setAddress(persistentAddress);
        persistentConvention.setActive(transientConvention.getActive());
        persistentConvention.setDescription(transientConvention.getDescription());
        persistentConvention.setEmail(transientConvention.getEmail());
        persistentConvention.setLastUpdate(DateUtils.getCurrentDate());
        persistentConvention.setName(transientConvention.getName());
        persistentConvention.setPhone(transientConvention.getPhone());
        persistentConvention.setWebsiteLink(transientConvention.getWebsiteLink());
		
		conventionDao.merge(persistentConvention);
	}
	
	@Transactional
	@Rollback
	public void deleteConvention(UserDetails userDetails, Long idConventionToDelete) throws Exception{
		
		MemberAccount persistentCurrentAccount=memberAccountDao.findByLogin(userDetails.getUsername());
		  
		if(persistentCurrentAccount==null){
				throw new Exception("Account errato");
		}
		
		Convention persistentConvention=conventionDao.findById(idConventionToDelete);
			
			if(persistentConvention==null){
				throw new Exception("convenzione non presente");
			}
			
			conventionDao.delete(persistentConvention);	
	}
	
	@Transactional
	@Rollback
	public List<Convention> getAllConventions(){
		
		List<Convention> allConventions=conventionDao.findAll();
		return allConventions;
	}
	
	@Transactional
	@Rollback
	public List<Convention> getByActive(boolean isActive){
		return conventionDao.findByActive(isActive);
	}
	
	@Transactional
	@Rollback
	public Convention getById(Long idConvention) throws Exception{
		
		  Convention persistentConvention=conventionDao.findById(idConvention);
		  if(persistentConvention==null){
			  throw new Exception("Convenzione non presente");
		  }
		  return persistentConvention;
	}
}
